//
//  NLCellViewModel.swift
//  sinaWeibo7110
//
//  Created by 安静清晨 on 2017/1/10.
//  Copyright © 2017年 walk-in-minds. All rights reserved.
//

import UIKit

class NLHomeCellViewModel: NSObject {
    
    var source : String?
    
    var verified_typeImage : UIImage?
    
    var mbrankImage : UIImage?
    
    var time : String?{
        
        
        let releaseTime = self.statuses?.created_at ?? ""
        print("releaseTime",releaseTime)
        //先格式化为可读字符串, 再转化为交互语言(刚刚, 几分钟前...
        let formatter = DateFormatter()
        //Sat Jan 14 11:21:47 +0800 2017
        formatter.dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy"
        //设置本地化
        formatter.locale = Locale(identifier: "en")
        guard let sinaDate = formatter.date(from: releaseTime) else{
            
            return ""
        }
        
        print("sinaDate=",sinaDate)
        
        
        
        //判断是否是今年
        //日历对象提供而非常丰富的日期处理函数
        let calender = Calendar.current
        //获取当前的日期
        let currentDate = Date()
        //通过比较当前日期和微博发布日期判断是否是今年
        let result = calender.dateComponents([.year], from: sinaDate, to: currentDate)
        if result.year == 0 {
            //今年
            if calender.isDateInToday(sinaDate) {
                //今天
                //获取sinaDate和当前日期隔了多少秒
                let delta = currentDate.timeIntervalSince(sinaDate)
                if delta < 60 {
                    //刚刚
                    return "刚刚"
                } else if delta > 60 && delta < 3600 {
                    return "\(Int(delta / 60))分钟前"
                } else {
                    //多少小时前
                    return "\(Int(delta / 3600))小时前"
                }
            } else {
                //非今天 昨天 和早于昨天
                if calender.isDateInYesterday(sinaDate) {
                    //昨天 HH:mm
                    formatter.dateFormat = "昨天 HH:mm"
                    return formatter.string(from: sinaDate)
                } else {
                    //早于昨天的 MM-dd
                    formatter.dateFormat = "MM-dd"
                    return formatter.string(from: sinaDate)
                }
            }
        }else {
            //非今年
            formatter.dateFormat = "yyyy-MM-dd"
            return formatter.string(from: sinaDate)
        }
        

        
        
        
    }
    
    //写一个get方法, 判断原创or转发
    //在下载单张图片时调用,比较方便
    var picInfos : [NLPicture]? {
        
        // 原创
        if self.statuses?.retweeted_status == nil {
            
            return self.statuses?.pic_urls
            
        }else{
            //转发
            return self.statuses?.retweeted_status?.pic_urls
        }
        
        
        
    }
    
    var statuses : NLStatusesModel? {
        
        didSet{
            
            getVertifiedType()
            getMBrank()
            processSource()
            
        }
        
    }
    
    
    func processSource(){
        
        let originalSource = self.statuses?.source ?? ""
        let startFlag = ">"
        let endFlag = "</a>"
        
        guard let startRange = originalSource.range(of:startFlag),
            let endRange = originalSource.range(of: endFlag) else {
            return }
        //lowerBound 表示范围的最小边界
        //upperBound 表示范围的最大边界
        let startIndex = startRange.upperBound
        let endIndex = endRange.lowerBound
        let subStr = originalSource.substring(with: startIndex..<endIndex)
        self.source = "来自" + subStr
        
    }
    
    
    
    
    
    
    
    func getVertifiedType(){
        
        //认证类型: -1：没有认证，0：认证用户，2,3,5: 企业认证，220: 达人(草根)
        let  verifiedType = statuses?.user?.verified_type ?? -1
        switch verifiedType {
        case 0:
            
            verified_typeImage = #imageLiteral(resourceName: "avatar_vip")
            
        case 2,3,5:
            
            verified_typeImage = #imageLiteral(resourceName: "avatar_enterprise_vip")
            
        case 220:
            
            verified_typeImage = #imageLiteral(resourceName: "avatar_grassroot")
            
        default:
            print("")
            //
            //            self.verified_typeImage? = nil
        }
        
    }
    
    private func getMBrank(){
        
        
        let mbrank = statuses?.user?.mbrank ?? 0
        if mbrank > 0 && mbrank < 7 {
            
            mbrankImage = UIImage(named: "common_icon_membership_level\(mbrank)")
            //print("mbrankImage = ",mbrankImage!)
        }
        
        
        
    }
    
    
    
}
